package cn.com.miao.lynx.common.core.util

import org.springframework.beans.factory.DisposableBean
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.springframework.context.ApplicationEvent
import org.springframework.stereotype.Component

/**
 * SpringContextUtil
 * spring上下文工具类
 * @title: SpringContextUtil
 * @description:
 * @author: dengmiao
 * @create: 2019-12-23 16:40
 **/
@Component
class SpringContextUtil: ApplicationContextAware, DisposableBean {

    companion object {

        @JvmField
        var applicationContext:ApplicationContext? = null

        @JvmStatic
        fun getBean(name: String): Any? {
            return applicationContext?.getBean(name)
        }

        @JvmStatic
        fun <T> getBean(clazz: Class<T>): Any? {
            return applicationContext?.getBean(clazz)
        }

        @JvmStatic
        fun publishEvent(event: ApplicationEvent) {
            applicationContext?.publishEvent(event)
        }
    }

    override fun setApplicationContext(p0: ApplicationContext) {
        applicationContext = p0
    }

    override fun destroy() {
        applicationContext = null
    }
}
